<!--
 * @Descripttion:
 * @version: 1.0.0
 * @Author: htang
 * @Date: 2023-12-08 15:39:08
 * @LastEditors: htang
 * @LastEditTime: 2023-12-08 17:04:35
-->
<template>
  <div class="jeecg-basic-table project-list-view">
    <div class="tip">
      <span>项目管理列表</span>
      <span class="tip-img"></span>
    </div>
    <div class="jeecg-search-layer">
      <a-form ref="form" :model="queryParam">
        <a-row :gutter="24">
          <a-col :span="8">
            <a-form-item label="项目编号">
              <a-input v-model:value="queryParam.projectCode" placeholder="请输入项目编号" allowClear />
            </a-form-item>
          </a-col>
          <a-col :span="8">
            <a-form-item label="项目名称">
              <a-input v-model:value="queryParam.projectName" placeholder="请输入项目名称" allowClear />
            </a-form-item>
          </a-col>
          <a-col :span="8">
            <a-form-item label="项目起止时间" name="projectStartTime">
              <a-range-picker
                v-model:value="datePicker"
                format="YYYY-MM-DD HH:mm:ss"
                valueFormat="YYYY-MM-DD HH:mm:ss"
                show-time
                style="width: 100%"
              />
            </a-form-item>
          </a-col>
          <a-col :span="8">
            <a-form-item label="甲方单位">
              <a-input v-model:value="queryParam.owner" placeholder="请输入甲方单位" allowClear />
            </a-form-item>
          </a-col>
          <a-col :span="8">
            <a-form-item label="项目实施负责人">
              <a-input v-model:value="queryParam.projectImplementOwner" placeholder="请输入项目实施负责人" allowClear />
            </a-form-item>
          </a-col>
          <a-col :span="8">
            <a-form-item label="研发负责人">
              <a-input v-model:value="queryParam.projectTechOwner" placeholder="请输入研发负责人" allowClear />
            </a-form-item>
          </a-col>
        </a-row>
        <a-row>
          <a-col :span="24" style="text-align: right">
            <a-form-item>
              <a-button type="primary" @click="onSearch">查询</a-button>
              <a-divider type="vertical" />
              <a-button type="primary" @click="onAdd">新增</a-button>
            </a-form-item>
          </a-col>
        </a-row>
      </a-form>
    </div>
    <div class="jeecg-table-view">
      <a-table :loading="loading" rowKey="id" :dataSource="dataSource" :columns="columns" :pagination="false">
        <template v-slot:bodyCell="{ column, record, index }">
          <template v-if="column.dataIndex == 'action'">
            <a @click="onDetail(record)">查看</a>
            <a-divider type="vertical" />
            <a style="color: red" @click="onDelete(record)">删除</a>
          </template>
        </template>
      </a-table>
      <Pagination
        v-model:current="ipagination.current"
        :pageSize="ipagination.pageSize"
        :total="ipagination.total"
        show-less-items
        @change="handlePageChange"
        :hideOnSinglePage="true"
      />
    </div>
    <FromDrawer ref="fromDrawer" @oks="init" />
  </div>
</template>

<script lang="ts" setup>
import { watch, ref, getCurrentInstance, nextTick } from 'vue';
import { projectPageApi, projectDeleteApi } from '@/api/project';
import { Pagination } from 'ant-design-vue';
import FromDrawer from './components/form.vue';
import { COLUMNS_CONFIG as columnsConfig } from './colums-config';

let { proxy } = getCurrentInstance();

let loading = ref(false);

let queryParam = ref({
  // 项目编码
  projectCode: '',
  // 项目名称
  projectName: '',
  // 甲方单位
  owner: '',
  // 项目建立开始时间
  projectStartTime: '',
  // 项目建立结束时间
  projectEndTime: '',
  // 项目实施负责人
  projectImplementOwner: '',
  // 研发负责人
  projectTechOwner: '',
});

let datePicker = ref([]);

watch(
  () => datePicker.value,
  ([a, b]) => {
    Object.assign(queryParam.value, {
      projectStartTime: a,
      projectEndTime: b,
    });
  }
);

let ipagination = ref({
  current: 1,
  pageSize: 10,
  pageSizeOptions: ['10', '20', '30'],
  showTotal: (total, range) => {
    return range[0] + '-' + range[1] + ' 共' + total + '条';
  },
  showQuickJumper: true,
  showSizeChanger: true,
  total: 0,
});

let dataSource = ref([]);

let columns = ref(columnsConfig);

function init() {
  let { current, pageSize } = ipagination.value;
  let data = {
    current,
    size: pageSize,
  };
  let keys = Object.keys(queryParam.value);
  Object.values(queryParam.value).map((_, i) => {
    if (_ !== '') {
      data[keys[i]] = _;
    }
  });
  loading.value = true;
  projectPageApi(data).then((res) => {
    loading.value = false;
    let { records, size, current, total } = res;
    Object.assign(ipagination.value, {
      current,
      pageSize: size,
      total,
    });
    dataSource.value = records;
  });
}

init();

/**
 * 切换当前页数
 */
function handlePageChange(page) {
  ipagination.value.current = page;
  init();
}

/**
 * 搜索数据
 */
function onSearch() {
  ipagination.value.current = 1;
  init();
}

/**
 * 打开新增弹窗
 */
function onAdd() {
  proxy.$refs.fromDrawer.visible = true;
}

/**
 * 根据ID查询当前信息
 * @param {number} id
 */
function onDetail({ id }) {
  proxy.$refs.fromDrawer.visible = true;
  nextTick(() => {
    proxy.$refs.fromDrawer.init(id);
  });
}

/**
 * 删除项目
 */
function onDelete({ id }) {
  projectDeleteApi({ id }).then((res) => {
    init();
  });
}
</script>

<style lang="less" scoped>
.project-list-view {
  margin: 12px;
  background: #fff;
  .tip {
    width: 100%;
    height: 44px;
    padding: 0 0 0 43.3px;
    background: url('@/assets/images/img-17.png') no-repeat;
    background-size: 100% 100%;
    line-height: 44px;
    span {
      font-size: 16px;
      color: #fff;
    }
  }
  .jeecg-search-layer {
    margin: 0 0 17px;
    padding: 12px 15px;
    background: #fff;
    border-radius: 8px;
    .ant-form {
      max-width: auto;
      .ant-form-item {
        margin-bottom: 24px;
      }
      :deep(.ant-col) {
        width: auto;
      }
    }
  }
}
</style>